/**
 * @author zhoushengtao
 * @since 2013-12-18 下午4:42:57
 */

package com.jason.android.core.builder;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class UrlBuilder {

	private String mHost;
	private String mScheme = "http";
	private String mPath = "";
	private String mPort = "";
	private Map<String, String> mParaMap = new HashMap<String, String>();

	public UrlBuilder setHost(String host) {
		mHost = host;
		return this;
	}

	public UrlBuilder setScheme(String mScheme) {
		this.mScheme = mScheme;
		return this;
	}

	public UrlBuilder setPort(String mPort) {
		this.mPort = mPort;
		return this;
	}

	public UrlBuilder addParam(String k, String v) {
		mParaMap.put(k, v);
		return this;
	}

	public String getUrl() {
		String url = mScheme + "://" + mHost;
		if (!TextUtils.isEmpty(mPort))
			url += ":" + mPort;

		if (!TextUtils.isEmpty(mPath))
			url += "/" + mPath;

		if (mParaMap.size() > 0) {

			String sigal = "?";
			Set<String> set = mParaMap.keySet();
			for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
				String key = iterator.next();
				url += sigal + key + "=" + mParaMap.get(key);
				if (sigal.equals("?"))
					sigal = "&";
			}

		}
		return url;
	}
}
